package com.qifenqian.turbot.module.adapter.restaurant

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.PagerAdapter
import android.view.ViewGroup
import com.qifenqian.turbot.module.entity.response.dish.CategoryEntity
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.qifenqian.turbot.module.ui.dish.TabDishesFragment

/**
 * @author H
 * @createDate 2018/6/1 15:03
 * @reference
 * @updateDate
 * @description [DishesManageActivity]
 * @since
 */
class DishManagePagerAdapter(fm: FragmentManager, private var categories: List<CategoryEntity>, private var fragmentList: List<Fragment>?) : FragmentStatePagerAdapter(fm) {

    private var mCategoryDishes: MutableList<MutableList<DishEntity>> = mutableListOf()
    private var mCategoryEntities: MutableList<CategoryEntity> = mutableListOf()

    fun setCategoryDishes(categoryDishes: MutableList<MutableList<DishEntity>>, categoryEntities: MutableList<CategoryEntity>) {
        mCategoryDishes.clear()
        mCategoryDishes = categoryDishes
        mCategoryEntities = categoryEntities
        notifyDataSetChanged()
    }

    override fun getItem(position: Int): Fragment {
        return fragmentList!![position]
    }

    //动态刷新的关键1
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val fragment = super.instantiateItem(container, position) as TabDishesFragment
        if (!mCategoryDishes.isEmpty()) {
            //动态刷新的关键3
            fragment.updateArguments(mCategoryDishes[position], mCategoryEntities, position)
        }
        return fragment
    }

    override fun getCount(): Int {
        return categories.size
    }

    //动态刷新的关键2
    override fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return categories[position].dishTypeName
    }

}
